package com.cml.common.dto.entity;


import com.alibaba.fastjson.JSONObject;
import com.cml.common.dto.message.ServiceRegisterRequest;

import java.util.Date;

/**
 * 姓名：陈茂林
 * 时间：2023/11/7 8:54
 * 描述：
 */
public class ServiceInstance {
    private String serviceName;

    private String ip;

    private Integer port;

    private Integer httpPort;



    private Date lastHearbeatTime;

    public Integer getHttpPort() {
        return httpPort;
    }

    public void setHttpPort(Integer httpPort) {
        this.httpPort = httpPort;
    }

    public Date getLastHearbeatTime() {
        return lastHearbeatTime;
    }

    public void setLastHearbeatTime(Date lastHearbeatTime) {
        this.lastHearbeatTime = lastHearbeatTime;
    }

    public String getServiceName() {
        return serviceName;
    }

    public void setServiceName(String serviceName) {
        this.serviceName = serviceName;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }

    @Override
    public String toString() {
        return "ServiceInstance{" +
                "serviceName='" + serviceName + '\'' +
                ", ip='" + ip + '\'' +
                ", port=" + port +
                '}';
    }



    public static class Builder{
        private ServiceInstance serviceInstance;
        private ServiceRegisterRequest serviceRegisterRequest;
        public Builder(){
            this.serviceInstance = new ServiceInstance();
        }

        public Builder setServiceRegisterRequest(ServiceRegisterRequest serviceRegisterRequest){
            this.serviceRegisterRequest=serviceRegisterRequest;
            return this;
        }
        public ServiceInstance build(){
            serviceInstance.setServiceName(serviceRegisterRequest.getServiceName());
            serviceInstance.setIp(serviceRegisterRequest.getIp());
            serviceInstance.setPort(serviceRegisterRequest.getPort());
            serviceInstance.setLastHearbeatTime(new Date());
            serviceInstance.setHttpPort(serviceRegisterRequest.getHttpPort());
            return this.serviceInstance;
        }

        public ServiceInstance build(JSONObject jsonObject){
            serviceInstance.setServiceName(jsonObject.getString("serviceName"));
            serviceInstance.setIp(jsonObject.getString("ip"));
            serviceInstance.setPort(jsonObject.getInteger("port"));
            serviceInstance.setLastHearbeatTime(jsonObject.getDate("lastHearbeatTime"));
            serviceInstance.setHttpPort(jsonObject.getInteger("httpPort"));
            return this.serviceInstance;
        }
    }
}
